function parseURL(url) {
    var a = document.createElement('a');
    a.href = url;
    var info = {
        source: url,
        protocol: a.protocol.replace(':', ''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function () {
            var ret = {};
            var seg = a.search.replace(/^\?/, '').split('&');
            var len = seg.length, s;
            for (var i = 0; i < len; i++) {
                if (!seg[i]) {
                    continue;
                }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
        hash: a.hash.replace('#', ''),
        path: a.pathname.replace(/^([^\/])/, '/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
        segments: a.pathname.replace(/^\//, '').split('/')
    };

    info.root = info.protocol + '://' + info.host
			+ (info.port ? (':' + info.port) : '') + '/';

    return info;
}

function rootURL() {
    return window.location.protocol + '//' + window.location.host + '/';
}

function startsWith(src, str) {
    return src.slice(0, str.length) == str;
}

function endsWith(src, str) {
    return src.indexOf(str, src.length - str.length) !== -1;
}

function _getViewModel($dlg) {
    return ko.contextFor($dlg.find('[data-bind]')[0]).$root;
}

function _browser(title, type, allowDownload, callback) {
    var $dlg = $('<div></div>');
    $(document.body).append($dlg);
    $dlg.load('/explorer/index.html', function () {
        var vm = _getViewModel($dlg);
        $dlg.dialog({
            title: title || 'Explorer',
            width: 760,
            height: 460,
            modal: true,
            resizable: true,
            close: function (event, ui) {
                $dlg.dialog('destroy');
                $dlg.remove();
            },
            open: function (event, ui) {
                vm.type(type);
                vm.allowDownload(allowDownload);
            },
            buttons: {
                OK: function () {
                    var file = vm.selectedPath();
                    $dlg.dialog('close');
                    if (type == 2 && callback) {
                        callback(file);
                    }
                },
                Cancel: function () {
                    $dlg.dialog('close');
                }
            }
        });
    });
}

function downloadBrowser(callback) {
    _browser('Download File', -1, true, callback);
}

function folderBrowser(callback) {
    _browser('Select Path', 2, false, callback);
}

function wmi(sql, callback) {
    $.getScript('/js/jscript_util.js', function (response, status) {
        var script = [response];
        $.getScript('/js/json.min.js', function (response, status) {
            script.push(response);
            jsonrpc.invoke({
                method: 'script',
                params: {
                    code: script.join(''),
                    expr: 'wmi("' + sql + '")'
                },
                success: function (data) {
                    callback(JSON.parse(data.result));
                }
            });
        });
    });
}